package com.threeox.commonlibrary.ui.view;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.threeox.commonlibrary.R;
import com.threeox.commonlibrary.config.CommonConfig;
import com.threeox.commonlibrary.config.inter.IListModelViewConfig;
import com.threeox.commonlibrary.config.inter.IModelViewConfig;
import com.threeox.commonlibrary.config.inter.IMultiPageModelConfig;
import com.threeox.commonlibrary.config.inter.ITableModelViewConfig;
import com.threeox.commonlibrary.inter.IOverallConfig;
import com.threeox.commonlibrary.ui.view.inter.engine.listmodel.INavigationView;
import com.threeox.commonlibrary.ui.view.inter.pullrefresh.IHeaderFooter;
import com.threeox.commonlibrary.ui.view.inter.reload.IReloadView;
import com.threeox.commonlibrary.ui.view.inter.table.ITablePagingView;
import com.threeox.commonlibrary.ui.view.inter.table.ITableView;
import com.threeox.commonlibrary.ui.view.inter.titlebar.ITitleBarView;
import com.threeox.commonlibrary.ui.view.loadview.ILoadDialog;
import com.threeox.commonlibrary.ui.view.loadview.LoadDialog;
import com.threeox.commonlibrary.ui.view.navigationview.CommonNavigationView;
import com.threeox.commonlibrary.ui.view.pullrefresh.headfooter.FooterView;
import com.threeox.commonlibrary.ui.view.pullrefresh.headfooter.HeaderView;
import com.threeox.commonlibrary.ui.view.reload.CommonReloadView;
import com.threeox.commonlibrary.ui.view.tableview.CommonTablePagingView;
import com.threeox.commonlibrary.ui.view.tableview.CommonTableView;
import com.threeox.utillibrary.util.EmptyUtils;
import com.threeox.utillibrary.util.res.LayoutUtils;

/**
 *
 * @ClassName: OverallInitView
 *
 * @Description: Todo(全局初始化的View)
 *
 * @author 赵屈犇
 *
 * @date 创建时间:2017/4/29 下午6:03
 *
 * @version 1.0
 */
public class OverallInitView {

    private Context mContext;
    private CommonConfig mCommonConfig;
    private IOverallConfig mOverallExtend;
    private IModelViewConfig mModelViewExtend;
    private IListModelViewConfig mListModelViewExtend;
    private ITableModelViewConfig mTableModelViewExtend;
    private IMultiPageModelConfig mMultiPageModelViewExtend;

    public static OverallInitView newInstance(Context context) {
        return new OverallInitView(context);
    }

    private OverallInitView(Context context) {
        this.mContext = context;
        mCommonConfig = CommonConfig.getInstance();
        mOverallExtend = mCommonConfig.getOverallExtend();
        mModelViewExtend = mCommonConfig.getModelViewExtend();
        mListModelViewExtend = mCommonConfig.getListModelViewExtend();
        mTableModelViewExtend = mCommonConfig.getTableModelViewExtend();
        mMultiPageModelViewExtend = mCommonConfig.getMultiPageViewExtend();
    }

    /*================列表模型各种View===================*/
    /**
     * 初始化列表的各种状态的View
     *
     * @return
     */
    public IReloadView initListReloadView() {
        IReloadView reloadView = null;
        if (null != mListModelViewExtend) {
            reloadView = mListModelViewExtend.initStatusView(mContext);
        }
        if (null != mOverallExtend) {
            reloadView = mOverallExtend.initStatusView(mContext);
        }
        if (null == reloadView) {
            reloadView = new CommonReloadView(mContext);
        }
        return reloadView;
    }

    /**
     * 初始化列表的标题栏
     *
     * @param titleName
     * @param rightText
     * @param leftText
     * @return
     */
    public ITitleBarView initListTitleBarView(String titleName, String rightText, String leftText) {
        ITitleBarView titleBarView = null;
        if (null != mListModelViewExtend) {
            titleBarView = mListModelViewExtend.initTitleBarView(mContext);
        }
        if (null != mOverallExtend) {
            titleBarView = mOverallExtend.initTitleBarView(mContext);
        }
        if (null == titleBarView) {
            titleBarView = initCommonTitleBarView(titleName, rightText, leftText);
        }
        return titleBarView;
    }

    /**
     * 初始化列表加载中的Dialog
     *
     * @return
     */
    public ILoadDialog initListModelLoadDialog() {
        ILoadDialog loadDialog = null;
        if (null != mListModelViewExtend) {
            loadDialog = mListModelViewExtend.initLoadDialog(mContext);
        }
        if (null != mOverallExtend) {
            loadDialog = mOverallExtend.initLoadDialog(mContext);
        }
        if (null == loadDialog) {
            loadDialog = new LoadDialog(mContext);
        }
        return loadDialog;
    }

    /**
     * 初始化列表侧边导航栏
     *
     * @return
     */
    public INavigationView initListNavigationView() {
        INavigationView navigationView = null;
        if (null != mListModelViewExtend) {
            navigationView = mListModelViewExtend.initNavigationView(mContext);
        }
        if (null == navigationView) {
            navigationView = new CommonNavigationView(mContext);
            // View view = LayoutUtils.inflate(mContext, R.layout.view_navigationview);
        }
        return navigationView;
    }

    /**
     * 初始化侧标导航栏的提示View
     *
     * @return
     */
    public TextView initNavigationDialogView() {
        TextView textView = null;
        if (null != mListModelViewExtend) {
            textView = mListModelViewExtend.initNavigationDialogView(mContext);
        }
        if (null == textView) {
            View view = LayoutUtils.inflate(mContext, R.layout.view_navigationview_dialog);
            return (TextView) view.findViewById(R.id.id_navigation_dialog_view);
        }
        return textView;
    }

    /**
     * 初始化上拉和下拉的头部View
     *
     * @return
     */
    public IHeaderFooter initPullToRefreshHeaderView() {
        IHeaderFooter headerView = null;
        if (null != mListModelViewExtend) {
            headerView = mListModelViewExtend.initPullToRefreshHeaderView(mContext);
        }
        if (null == headerView) {
            headerView = new HeaderView(mContext);
        }
        return headerView;
    }

    /**
     * 初始化上拉和下拉的底部View
     *
     * @return
     */
    public IHeaderFooter initPullToRefreshFooterView() {
        IHeaderFooter footerView = null;
        if (null != mListModelViewExtend) {
            footerView = mListModelViewExtend.initPullToRefreshFooterView(mContext);
        }
        if (null == footerView) {
            footerView = new FooterView(mContext);
        }
        return footerView;
    }

    /*================列表模型各种View===================*/

    /*================页面模型各种View===================*/
    /**
     * 初始化模型的各种状态的View
     *
     * @return
     */
    public IReloadView initModelReloadView() {
        IReloadView reloadView = null;
        if (null != mModelViewExtend) {
            reloadView = mModelViewExtend.initStatusView(mContext);
        }
        if (null != mOverallExtend) {
            reloadView = mOverallExtend.initStatusView(mContext);
        }
        if (null == reloadView) {
            reloadView = new CommonReloadView(mContext);
        }
        return reloadView;
    }

    /**
     * 初始化模型的标题栏
     *
     * @param titleName
     * @param rightText
     * @param leftText
     * @return
     */
    public ITitleBarView initModelTitleBarView(String titleName, String rightText, String leftText) {
        ITitleBarView titleBarView = null;
        if (null != mModelViewExtend) {
            titleBarView = mModelViewExtend.initTitleBarView(mContext);
        }
        if (null != mOverallExtend) {
            titleBarView = mOverallExtend.initTitleBarView(mContext);
        }
        if (null == titleBarView) {
            titleBarView = initCommonTitleBarView(titleName, rightText, leftText);
        }
        return titleBarView;
    }

    /**
     * 初始化模型加载中的Dialog
     *
     * @return
     */
    public ILoadDialog initModelLoadDialog() {
        ILoadDialog loadDialog = null;
        if (null != mModelViewExtend) {
            loadDialog = mModelViewExtend.initLoadDialog(mContext);
        }
        if (null != mOverallExtend) {
            loadDialog = mOverallExtend.initLoadDialog(mContext);
        }
        if (null == loadDialog) {
            loadDialog = new LoadDialog(mContext);
        }
        return loadDialog;
    }
    /*================页面模型各种View===================*/

    /*================表格模型各种View===================*/
    /**
     * 初始化表格模型的标题栏
     *
     * @param titleName
     * @param rightText
     * @param leftText
     * @return
     */
    public ITitleBarView initTableModelTitleBarView(String titleName, String rightText, String leftText) {
        ITitleBarView titleBarView = null;
        if (null != mTableModelViewExtend) {
            titleBarView = mTableModelViewExtend.initTitleBarView(mContext);
        }
        if (null != mOverallExtend) {
            titleBarView = mOverallExtend.initTitleBarView(mContext);
        }
        if (null == titleBarView) {
            titleBarView = initCommonTitleBarView(titleName, rightText, leftText);
        }
        return titleBarView;
    }

    /**
     * 初始化表格模型的各种状态的View
     *
     * @return
     */
    public IReloadView initTableModelReloadView() {
        IReloadView reloadView = null;
        if (null != mTableModelViewExtend) {
            reloadView = mTableModelViewExtend.initStatusView(mContext);
        }
        if (null != mOverallExtend) {
            reloadView = mOverallExtend.initStatusView(mContext);
        }
        if (null == reloadView) {
            reloadView = new CommonReloadView(mContext);
        }
        return reloadView;
    }

    /**
     * 初始化表格模型的表格View
     *
     * @return
     */
    public ITableView initTableModelTableView() {
        ITableView tableView = null;
        if (null != mTableModelViewExtend) {
            tableView = mTableModelViewExtend.initTableView(mContext);
        }
        if (null != mOverallExtend) {
            tableView = mOverallExtend.initTableView(mContext);
        }
        if (null == tableView) {
            tableView = new CommonTableView(mContext);
        }
        return tableView;
    }

    /**
     * 初始化表格模型的表格分页View
     *
     * @return
     */
    public ITablePagingView initTablePagingView() {
        ITablePagingView tablePagingView = null;
        if (null != mTableModelViewExtend) {
            tablePagingView = mTableModelViewExtend.initTablePagingView(mContext);
        }
        if (null != mOverallExtend) {
            tablePagingView = mOverallExtend.initTablePagingView(mContext);
        }
        if (null == tablePagingView) {
            tablePagingView = new CommonTablePagingView(mContext);
        }
        return tablePagingView;
    }

    /**
     * 初始化表格模型的加载中的Dialog
     *
     * @return
     */
    public ILoadDialog initTableModelLoadDialog() {
        ILoadDialog loadDialog = null;
        if (null != mTableModelViewExtend) {
            loadDialog = mTableModelViewExtend.initLoadDialog(mContext);
        }
        if (null != mOverallExtend) {
            loadDialog = mOverallExtend.initLoadDialog(mContext);
        }
        if (null == loadDialog) {
            loadDialog = new LoadDialog(mContext);
        }
        return loadDialog;
    }

    /*================表格模型各种View===================*/

    /*================多页面模型各种View===================*/
    /**
     * 初始化多页面模型的标题栏
     *
     * @param titleName
     * @param rightText
     * @param leftText
     * @return
     */
    public ITitleBarView initMultiPageTitleBarView(String titleName, String rightText, String leftText) {
        ITitleBarView titleBarView = null;
        if (null != mMultiPageModelViewExtend) {
            titleBarView = mMultiPageModelViewExtend.initTitleBarView(mContext);
        }
        if (null != mOverallExtend) {
            titleBarView = mOverallExtend.initTitleBarView(mContext);
        }
        if (null == titleBarView) {
            titleBarView = initCommonTitleBarView(titleName, rightText, leftText);
        }
        return titleBarView;
    }

    /**
     * 初始化多页面模型的各种状态的View
     *
     * @return
     */
    public IReloadView initMultiPageReloadView() {
        IReloadView reloadView = null;
        if (null != mMultiPageModelViewExtend) {
            reloadView = mMultiPageModelViewExtend.initStatusView(mContext);
        }
        if (null != mOverallExtend) {
            reloadView = mOverallExtend.initStatusView(mContext);
        }
        if (null == reloadView) {
            reloadView = new CommonReloadView(mContext);
        }
        return reloadView;
    }

    /**
     * 初始化多页面模型加载中的Dialog
     *
     * @return
     */
    public ILoadDialog initMultiPageLoadDialog() {
        ILoadDialog loadDialog = null;
        if (null != mMultiPageModelViewExtend) {
            loadDialog = mMultiPageModelViewExtend.initLoadDialog(mContext);
        }
        if (null != mOverallExtend) {
            loadDialog = mOverallExtend.initLoadDialog(mContext);
        }
        if (null == loadDialog) {
            loadDialog = new LoadDialog(mContext);
        }
        return loadDialog;
    }

    /*================多页面模型各种View===================*/

    /**
     * 初始化全局的Dialog
     *
     * @return
     */
    public ILoadDialog initOverallLoadDialog() {
        ILoadDialog loadDialog = null;
        if (null != mOverallExtend) {
            loadDialog = mOverallExtend.initLoadDialog(mContext);
        }
        if (null == loadDialog) {
            loadDialog = new LoadDialog(mContext);
        }
        return loadDialog;
    }

    /**
     * 初始化类库的标题View
     *
     * @param titleName
     * @param rightText
     * @param leftText
     * @return
     */
    private ITitleBarView initCommonTitleBarView(String titleName, String rightText, String leftText) {
        ITitleBarView titleBarView = new CommonTitleBarView(mContext);
        CommonTitleBarView commonTitleBarView = ((CommonTitleBarView) titleBarView);
        commonTitleBarView.init(CommonTitleBarView.TopBarStyle.showLeft, titleName);
        if (EmptyUtils.isNotEmpty(rightText)) {
            commonTitleBarView.setLayout(R.layout.title_bar_text, CommonTitleBarView.LayoutStyle.right);
            TextView textView = (TextView) commonTitleBarView.findViewById(R.id.id_model_titlebar_text);
            if (textView != null)
                textView.setText(rightText);
        }
        if (EmptyUtils.isNotEmpty(leftText)) {
            commonTitleBarView.setLayout(R.layout.title_bar_text, CommonTitleBarView.LayoutStyle.left);
            TextView textView = (TextView) commonTitleBarView.findViewById(R.id.id_model_titlebar_text);
            if (textView != null)
                textView.setText(leftText);
        }
        return titleBarView;
    }

}
